#pragma once;

#include "physics_object.h"
#include "common.h"
#include "collision_primitive.h"
#include "collisiondetector.h"

namespace TorPhysix
{

class Box : public PhysicsObject
{
public:
	Box() : PhysicsObject()
	{
		collisionBox = new TorPhysix::CollisionBox;
		this->collisionPrimitive = collisionBox;
	}

	virtual ~Box()
	{
		//std::cout << "box dtor" << std::endl;
		delete collisionBox;
	}

	virtual void vRender();
	virtual void vPicking();
	void vRenderShadow();

	void vSetState(real x, real y, real z, Quatf orientation, Vec3f collisionSize, real mass, Vec3f acceleration)
	{
		setIdentity( collisionPrimitive->offset ); //  no offset from rigid body origin

		collisionBox->body->setPosition( Vec3f( x, y, z ) );
		collisionBox->body->setOrientation( orientation );
		collisionBox->body->setVelocity( Vec3f(0.0f, 0.0f, 0.0f) );
		collisionBox->body->setRotation( Vec3f(0.0f, 0.0f, 0.0f) );
		collisionBox->body->setAcceleration( acceleration );

		collisionBox->halfSize = collisionSize;

		Mat3f cuboidTensor;
		setIdentity(cuboidTensor);
		cuboidTensor[0] = 1.0f / 12.0f * mass * ( collisionSize.y()*collisionSize.y() + collisionSize.z()*collisionSize.z() );
		cuboidTensor[4] = 1.0f / 12.0f * mass * ( collisionSize.x()*collisionSize.x() + collisionSize.z()*collisionSize.z() );
		cuboidTensor[8] = 1.0f / 12.0f * mass * ( collisionSize.y()*collisionSize.y() + collisionSize.y()*collisionSize.y() );
		collisionBox->body->setInertiaTensor( cuboidTensor );

		collisionBox->body->setDamping(0.80f, 0.65f);
		collisionBox->body->setMass(mass);
		collisionBox->body->clearAccumulators();
		collisionBox->body->calculateDerivedData();
		collisionBox->calculateInternals();
	}
	virtual void vRenderDebug();

	virtual void vScale( Vec3f scaleAxis, float scaleMagnitude );
	virtual void vInteractWith( SceneObject *mutator ) {}

	PhysicsObject* vGetPhysicsObject() const { return 0x0; }  // phy obj
	virtual bool isBox(){ return true; }



	virtual void acceptVisitor( PhysicsObject& visitor, CollisionData* cData ){ visitor.collideAgainst( *this, cData ); }
	virtual void collideAgainst( Box& box, CollisionData* cData );
	virtual void collideAgainst( Ball& ball, CollisionData* cData );
	virtual void collideAgainst( TorPhysix::CollisionPlane& plane, CollisionData* cData ) { /*std::cout << "box vs plane" << std::endl;*/ }
public:
	TorPhysix::CollisionBox *collisionBox;
	
};

}
